package com.quectel.cms.controller.ai;

import cn.hutool.core.date.DateUtil;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.ai.dto.mongo.MongoAiEventRecordDto;
import com.quectel.core.module.ai.service.mongo.MongoAiEventRecordService;
import com.quectel.util.common.DateUtils;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-22 11:47:45
 */
@RestController
@RequestMapping("aiEventRecord")
@Tag(name = "AiEventRecordController", description = "视频事件记录相关api")
public class AiEventRecordController extends BaseController {

    @DubboReference
    private MongoAiEventRecordService aiEventRecordService;

    public static final String EVENT_TYPES = "eventTypes";

    /**
     * 列表
     */
    @GetMapping("pageList")
    @Operation(summary = "视频事件记录的分页查询列表")
    public Response<List<MongoAiEventRecordDto>> pageList(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        String startTime = request.getString(SystemConstants.TIME_SLOT_KEY_PAIR.getLeft());
        String endTime = request.getString(SystemConstants.TIME_SLOT_KEY_PAIR.getRight());

        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            if (Objects.requireNonNull(DateUtils.parse(endTime, DateUtils.FormatType.COMMON)).getTime()
                    - Objects.requireNonNull(DateUtils.parse(startTime, DateUtils.FormatType.COMMON)).getTime() > 1000 * 60 * 60 * 24 * 32L) {
                return Response.error("最长记录不能超过30天");
            }
        } else {
            request.put(SystemConstants.TIME_SLOT_KEY_PAIR.getLeft(), DateUtils.format(DateUtil.offsetDay(new Date(), -7), DateUtils.FormatType.COMMON));
            request.put(SystemConstants.TIME_SLOT_KEY_PAIR.getLeft(), DateUtils.format(new Date(), DateUtils.FormatType.COMMON));
        }

        super.fillAuthorityData(request.getParams());
        if (request.isNotBlank(EVENT_TYPES)) {
            request.put(EVENT_TYPES, request.getString(EVENT_TYPES).split(","));
        }
        return Response.<List<MongoAiEventRecordDto>>ok().wrap(aiEventRecordService.queryList(request.getParams()));
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "aiEventRecord:list")
    @Operation(summary = "视频事件记录的分页查询列表")
    public Response<Page<MongoAiEventRecordDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        String startTime = request.getString(SystemConstants.TIME_SLOT_KEY_PAIR.getLeft());
        String endTime = request.getString(SystemConstants.TIME_SLOT_KEY_PAIR.getRight());

        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            if (Objects.requireNonNull(DateUtils.parse(endTime, DateUtils.FormatType.COMMON)).getTime()
                    - Objects.requireNonNull(DateUtils.parse(startTime, DateUtils.FormatType.COMMON)).getTime() > 1000 * 60 * 60 * 24 * 32L) {
                return Response.error("最长记录不能超过30天");
            }
        } else {
            request.put(SystemConstants.TIME_SLOT_KEY_PAIR.getLeft(), DateUtils.format(DateUtil.offsetDay(new Date(), -7), DateUtils.FormatType.COMMON));
            request.put(SystemConstants.TIME_SLOT_KEY_PAIR.getLeft(), DateUtils.format(new Date(), DateUtils.FormatType.COMMON));
        }

        super.fillAuthorityData(request.getParams());
        if (request.isNotBlank(EVENT_TYPES)) {
            request.put(EVENT_TYPES, request.getString(EVENT_TYPES).split(","));
        }
        int total = aiEventRecordService.queryTotal(request.getParams());

        List<MongoAiEventRecordDto> list = null;
        if (total > 0) {
            list = aiEventRecordService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<MongoAiEventRecordDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<MongoAiEventRecordDto>>ok().wrap(page);
    }


    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "aiEventRecord:info")
    @Operation(summary = "查询单条视频事件记录")
    public Response<MongoAiEventRecordDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (aiEventRecordService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        MongoAiEventRecordDto aiEventRecordDto = aiEventRecordService.selectById(id);

        return Response.<MongoAiEventRecordDto>ok().wrap(aiEventRecordDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "aiEventRecord:edit")
    @Operation(summary = "保存单条视频事件记录")
    public Response<Object> save(@RequestBody @Validated MongoAiEventRecordDto aiEventRecordDto) {

        //填充基础数据
        fillSaveCommonData(aiEventRecordDto);

        aiEventRecordService.save(aiEventRecordDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "aiEventRecord:delete")
    @Operation(summary = "批量删存在的除视频事件记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (aiEventRecordService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        aiEventRecordService.deleteBatch(ids);

        return Response.ok();
    }
}
